package com.itheima.users.ui;

import com.itheima.chap04.Result;
import com.itheima.users.entity.User;
import com.itheima.users.service.UserService;

import java.util.Scanner;

public class UserCMDUI implements UserUI {
    private Scanner sc = new Scanner(System.in);
    private UserService userService;

    public UserCMDUI(UserService userService) {
        this.userService = userService;
    }

    @Override
    public void entrance() {

        while (true) {
            System.out.println("欢迎来到用户管理系统");
            System.out.println("1.注册");
            System.out.println("2.登录");
            System.out.println("3.退出");
            System.out.println("请输入您的选择：");

            int option = sc.nextInt();
            switch (option) {
                case 1:
                    registerUI();
                    break;
                case 2:
                    loginUI();
                    return;
                case 3:
                    System.out.println("谢谢使用");
                    return;
                default:
                    System.out.println("输入错误");
                    break;
            }
        }
    }

    private void registerUI() {
        while (true){
            // 1.打印注册菜单，录入用户信息
//            System.out.println("请输入用户名：");
//            String username = sc.next();
//            System.out.println("请输入密码：");
//            String password = sc.next();
//            System.out.println("请确认密码：");
//            String confirmPassword = sc.next();
            String username = nextString("请输入用户名：");
            String password = nextString("请输入密码：");
            String confirmPassword = nextString("请确认密码：");

            if (username == null || password == null || confirmPassword == null) {
                System.out.println("输入有误，用户名密码不能为空");
                continue;
            }

            // 2.判断密码是否一致
            if (!password.equals(confirmPassword)) {
                System.out.println("两次密码不一致，请重新输入");
                continue;
            }
            // 3.调用业务层，完成注册
            Result result = userService.register(username, password);
            // 4.处理结果
            if (result.issuccess()) {
                System.out.println("注册成功！");
                User user = (User) result.getData();
                System.out.println("用户编号：" + user.getId());
                System.out.println("用户名：" + user.getUsername());
                return;
            }
            System.out.println("注册失败！" + result.getMsg());
            // 5.如果失败，询问是否需要重试
            String option = nextString("是否需要重试？(y - 是/n - 否)");
            if (option.equalsIgnoreCase("n")){
                return;
            }

        }
    }

    private void loginUI() {
        while (true){
            // 1.录入信息
            String username = nextString("请输入用户名：");
            String password = nextString("请输入密码：");
            // 2.调用业务层，完成登录
            Result result = userService.login(username, password);
            // 3.处理结果
            if (result.issuccess()) {
                User user = (User) result.getData();
                System.out.println("欢迎回来，" + user.getUsername());
                return;
            }
            // 4.如果失败，询问是否需要重试
            System.out.println("登陆失败" + result.getMsg());
            String option = nextString("是否需要重试？(y - 是/n - 否)");
            if (option.equalsIgnoreCase("n")) {
                return;
            }
        }
    }

    private String nextString(String prompt){
        System.out.println(prompt);
        return sc.next();
    }
}